﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

namespace Muccu
{
    [AttributeUsage(AttributeTargets.Parameter)]
    public class RegexAttribute: ArgumentValidationAttribute
    {
        private string pattern;

        public string Pattern
        {
            get { return pattern; }
            set { pattern = value; }
        }
        
        public override bool ServerValidate(object value)
        {
            return Regex.IsMatch((string)value, pattern);
        }

        public override string ClientValidate
        {
            get
            {
                return @"function(val, messages){
var re = /"+pattern+ @"/; 
    var isValid = re.test(val);
    if(!isValid)messages.push(""" + ErrorMessage + "\"); return isValid;}";
            }
        }
    }
}
